package Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Timer;

import javax.swing.JOptionPane;

import ServerControl.DBHandler;
import ServerControl.TimerController;
import entities.MessageManager;
import entities.SystemUser;
import Common.Settings;
import ocsf.server.AbstractServer;
import ocsf.server.ConnectionToClient;


public class EchoServer extends AbstractServer 
{
	//Fields
	public Timer timer;
	private static DBHandler SqlDB;
	ServerConnectionGUI serverConnectionGUI ;
	
	
	//Ct'r
	public EchoServer(int port , ServerConnectionGUI serverConnectionGUI) 
	{
		super(port);
		this.serverConnectionGUI = serverConnectionGUI ;
		try {
			SqlDB = new DBHandler(Settings.DB_Host,Settings.DB_UserName,Settings.DB_Password, Settings.DB_Scheme, serverConnectionGUI);
		} catch (InstantiationException e) {
			
		} catch (IllegalAccessException e) {
			
		} catch (ClassNotFoundException e) {
			
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "ERROR: Cannot connect to DB", "Warning Message", JOptionPane.ERROR_MESSAGE);
			
		}
	}


	public static DBHandler getSqlDB(){
		return SqlDB;
	}

	@SuppressWarnings("incomplete-switch")
	public void handleMessageFromClient (Object msg, ConnectionToClient client)
	{
		MessageManager msgM = (MessageManager)msg;
		serverConnectionGUI.display("Message received: " + msg + " from " + client);
		try {
			switch(msgM.getMsgType())
			{

			case LOGIN_REQUEST:	{
				
				SystemUser systemUser = (SystemUser)msg;
				systemUser = SqlDB.getLoginDataByUserNameAndPassword(systemUser.getUserName(),systemUser.getUserPassword());
				client.sendToClient(systemUser);
				break;
			}


			case LOGOUT_REQUEST:{
				
				SystemUser systemUser = (SystemUser)msg;
				try {
					client.sendToClient(SqlDB.SetLogOut(systemUser.getUserName()));
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
			

			}
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			
		}

	}

	/**
	 * This method overrides the one in the superclass.  Called
	 * when the server starts listening for connections.
	 */
	protected void serverStarted()
	{
		serverConnectionGUI.display("Server listening for connections on port " + getPort());

	}

	/**
	 * This method overrides the one in the superclass.  Called
	 * when the server stops listening for connections.
	 */
	protected void serverStopped()
	{
		serverConnectionGUI.display("Server has stopped listening for connections.");

	}



	//Class methods ***************************************************

	/**
	 * This method is responsible for the creation of
	 * the server instance (there is no UI in this phase).
	 *
	 * @param args the arguments
	 */
/*	public static void main(String[] args) 
	{
		
		int port = 0; //Port to listen on
	}*/


	public int connect(int port, EchoServer sv) {
		// TODO Auto-generated method stub


		try 
		{
			sv.listen(); //Start listening for connections
		} 
		catch (Exception ex) 
		{
			serverConnectionGUI.display("ERROR - Could not listen for clients!");
			return 0;
		}


		//Timer Start!
		timer = new Timer("Timer");

		TimerController tx = new TimerController();
		//timer.schedule(tx, 0, 600000);
		//name,dealy since start, run every x time in ms
		serverConnectionGUI.display("Timer started");
		return 1;
	}
	
	public void disconnet(EchoServer sv){

		try 
		{
			sv.stopListening();//disconnect
			timer.cancel();
			serverConnectionGUI.display("Timer stopped");

		} 
		catch (Exception ex) 
		{
			serverConnectionGUI.display("ERROR - Could not listen for clients!");

		}


	}


}